package com.hnnou.freshmenappserver.controller;

import com.hnnou.freshmenappserver.bean.Announcement;
import com.hnnou.freshmenappserver.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})

public class AnnouncementController {
    @Autowired
    AnnouncementService announcementService;
    @RequestMapping("/freshmenapp/announcement/list")
    @ResponseBody
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<Announcement> list = announcementService.findAll();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list",list);

        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @RequestMapping("/freshmenapp/announcement/findone")
    @ResponseBody
    public Map<String, Object> findOneById(@RequestBody Announcement announcement) {
        Map<String, Object> map = new HashMap<>();
        Announcement announcement1 = announcementService.selectById(announcement.getId());
        if (announcement1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", announcement1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    @RequestMapping("/freshmenapp/announcement/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestBody Announcement announcement) {
        Map<String, Object> map = new HashMap<>();
        int result = announcementService.deleteById(announcement.getId());
        if(result>0){

            map.put("success", true);
            map.put("msg", "成功");
        } else{
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
    @RequestMapping("/freshmenapp/announcement/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Announcement announcement) {
        Map<String, Object> map = new HashMap<>();
        int count  = announcementService.update(announcement);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    // 添加
    @RequestMapping("/freshmenapp/announcement/addone")
    @ResponseBody
    public Map<String, Object> insert(@RequestBody Announcement announcement) {
        Map<String, Object> map = new HashMap<>();
        int count = announcementService.insert(announcement);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

}
